package com.itheima.web;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.bean.ShoppingCart;
import com.itheima.common.R;
import com.itheima.service.ShoppingCartService;
import com.itheima.utils.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.yaml.snakeyaml.events.Event;

import java.time.LocalDateTime;
import java.util.List;

@RequestMapping("/shoppingCart")
@RestController
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;


    //添加购物车
    @PostMapping("/add")
    public R add(@RequestBody ShoppingCart shoppingCart) {

        //补充数据
        shoppingCart.setUserId(BaseContext.getCurrentId());


        //添加购物车


        //得到查询购物车的数据
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();

        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());

        //设置菜品id或者套餐id
        if (shoppingCart.getDishId() != null) {

            lqw.eq(ShoppingCart::getDishId, shoppingCart.getDishId());

        } else {

            lqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());

        }

        //查询当前菜品或者套餐是否在购物车中
        //SQL:select * from shopping_cart where user_id = ? and dish_id/setmeal_id = ?
        ShoppingCart cartServiceOne = shoppingCartService.getOne(lqw);

        if (cartServiceOne != null) {
            //如果已经存在，就在原来数量基础上加一
            Integer number = cartServiceOne.getNumber();
            cartServiceOne.setNumber(number + 1);
            shoppingCartService.updateById(cartServiceOne);
        } else {
            //如果不存在，则添加到购物车，数量默认就是一
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            cartServiceOne = shoppingCart;
        }
        return R.success(cartServiceOne);
    }


    //查看购物车数据
    @GetMapping("/list")
    public R list() {

        //按照用户的id来查询购物车的数据.
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());

        //执行查询
        List<ShoppingCart> cartList = shoppingCartService.list(lqw);

        return R.success(cartList);

    }


    //清空购物车
    @DeleteMapping("/clean")
    public R clean() {
        //根据用户id来清空购物车数据
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();

        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());

        //直接做删除操作
        boolean result = shoppingCartService.remove(lqw);

        if (result) {

            return R.success("清空购物车成功");
        }
        return R.error("清空购物车失败");

    }


    //删除购物车
    @PostMapping("/sub")
    public R sub(@RequestBody ShoppingCart shoppingCart) {

        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();

        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());

        Long dishId = shoppingCart.getDishId();

        if (dishId != null) {
            lqw.eq(ShoppingCart::getDishId, dishId);
        } else {
            lqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        ShoppingCart one = shoppingCartService.getOne(lqw);

        Integer number = one.getNumber();

        one.setNumber(number - 1);

        shoppingCartService.updateById(one);

        return R.success(one);
    }


}
